﻿using System;
using System.Text.RegularExpressions;

namespace Kovan.Library.Utils
{
    /// <summary>
    /// 提供两个基本类型之间的转换及类型格式判断一序列方法
    /// </summary>
    public class KConvert
    {
        #region 类型转换
        public static char ToChar(object value, char dValue)
        {
            if (!KConvert.IsChar(value)) return dValue;
            return Convert.ToChar(value);
        }
        public static bool ToBoolean(object value)
        {
            return KConvert.ToBoolean(value, false);
        }
        public static bool ToBoolean(object value, bool dValue)
        {
            if (!KConvert.IsBoolean(value)) return dValue;
            return Convert.ToBoolean(value);
        }
        /// <summary>
        /// 数字转换为字母
        /// </summary>
        /// <param name="num">1转为A，2转为B，3转为C以此类推……</param>
        /// <returns></returns>
        public static string NumberToChar(int num)
        {
            int number = num + 64;
            if (65 <= number && 90 >= number)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] btNumber = new byte[] { (byte)number };
                return asciiEncoding.GetString(btNumber);
            }
            return "数字不在转换范围之内";
        }

        /// <summary>
        /// 转换为 System.String 类型,默认为 String.Empty
        /// </summary>
        /// <param name="value">一个对象 System.Object</param>
        public static string ToString(object value)
        {
            return KConvert.ToString(value, String.Empty);
        }
        public static string ToString(object value, string dValue)
        {
            if (KConvert.IsAbsEmpty(value)) return dValue;
            return value.ToString();
        }
        public static DateTime ToDateTime(object value)
        {
            return KConvert.ToDateTime(value, DateTime.MinValue);
        }
        public static DateTime ToDateTime(object value, DateTime dValue)
        {
            if (!KConvert.IsDateTime(value)) return dValue;
            return Convert.ToDateTime(value);
        }
        public static DateTime ConvertExifDateTime(object value)
        {
            if (KConvert.IsEmpty(value)) return DateTime.Now;
            string[] arr = KConvert.ToString(value).Trim().Split(new char[] { ' ' });
            return KConvert.ToDateTime(arr[0].Replace(':', '-') + " " + arr[1], DateTime.Now);
        }
        public static byte ToByte(object value)
        {
            return KConvert.ToByte(value, 0);
        }
        public static byte ToByte(object value, byte dValue)
        {
            if (!KConvert.IsByte(value)) return dValue;
            return Convert.ToByte(value);
        }
        public static short ToShort(object value)
        {
            return KConvert.ToShort(value, 0);
        }
        public static short ToShort(object value, short dValue)
        {
            if (!KConvert.IsShort(value)) return dValue;
            return Convert.ToInt16(value);
        }
        public static int ToInt(object value)
        {
            return KConvert.ToInt(value, 0);
        }
        public static int ToInt(object value, int dValue)
        {
            if (!KConvert.IsInt(value)) return dValue;
            return Convert.ToInt32(value);
        }
        public static int ToInt32(object value)
        {
            return KConvert.ToInt(value);
        }
        public static int ToInt32(object value, int dValue)
        {
            return KConvert.ToInt(value, dValue);
        }

        public static float ToSingle(object value)
        {
            return KConvert.ToSingle(value, 0);
        }
        public static float ToSingle(object value, float dValue)
        {
            if (!KConvert.IsSingle(value)) return dValue;
            return Convert.ToSingle(value);
        }
        public static double ToDouble(object value)
        {
            return KConvert.ToDouble(value, 0);
        }
        public static double ToDouble(object value, double dValue)
        {
            if (!KConvert.IsDouble(value)) return dValue;
            return Convert.ToDouble(value);
        }
        public static decimal ToDecimal(object value)
        {
            return KConvert.ToDecimal(value, 0);
        }
        public static decimal ToDecimal(object value, decimal dValue)
        {
            if (!KConvert.IsDecimal(value)) return dValue;
            return Convert.ToDecimal(value);
        }
        public static long ToLong(object value)
        {
            return ToLong(value, 0);
        }
        public static long ToLong(object value, long dValue)
        {
            if (!KConvert.IsLong(value)) return dValue;
            return Convert.ToInt64(value);
        }
        public static string IntToIP(long ip_Int)
        {
            long seg1 = (ip_Int & 0xff000000) >> 24;
            if (seg1 < 0)
                seg1 += 0x100;
            long seg2 = (ip_Int & 0x00ff0000) >> 16;
            if (seg2 < 0)
                seg2 += 0x100;
            long seg3 = (ip_Int & 0x0000ff00) >> 8;
            if (seg3 < 0)
                seg3 += 0x100;
            long seg4 = (ip_Int & 0x000000ff);
            if (seg4 < 0)
                seg4 += 0x100;
            string ip = seg1.ToString() + "." + seg2.ToString() + "." + seg3.ToString() + "." + seg4.ToString();

            return ip;
        }
        public static long IPToInt(string ip)
        {
            ip = KConvert.ToString(ip).Trim();
            char[] dot = new char[] { '.' };
            string[] ipArr = ip.Split(dot);
            if (ipArr.Length == 3)
                ip = ip + ".0";
            ipArr = ip.Split(dot);
            long ip_Int = 0;
            long p1 = long.Parse(ipArr[0]) * 256 * 256 * 256;
            long p2 = long.Parse(ipArr[1]) * 256 * 256;
            long p3 = long.Parse(ipArr[2]) * 256;
            long p4 = long.Parse(ipArr[3]);
            ip_Int = p1 + p2 + p3 + p4;
            return ip_Int;
        }
        #endregion

        #region 类型格式判断
        /// <summary>
        /// 判断对象是否为Null
        /// </summary>
        /// <param name="value">待判断对象</param>
        public static bool IsNull(object value)
        {
            if ((value != null) && !Convert.IsDBNull(value))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 判断值类型对象是否有数据
        /// </summary>
        /// <param name="value">待判断值类型对象</param>
        public static bool IsAbsEmpty(object value)
        {
            if (IsNull(value)) return true;
            if (String.IsNullOrEmpty(value.ToString())) return true;
            return false;
        }
        /// <summary>
        /// 判断值类型对象是否有数据(不计算前后空格)
        /// </summary>
        /// <param name="value">待判断值类型对象</param>
        public static bool IsEmpty(object value)
        {
            if (IsNull(value)) return true;
            if (String.IsNullOrEmpty(value.ToString().Trim())) return true;
            return false;
        }
        /// <summary>
        /// 判断是否为图片文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool IsimagesFilename(string filename)
        {
            if (KConvert.IsEmpty(filename)) return false;
            filename = filename.Trim();
            if (filename.EndsWith(".") || filename.IndexOf(".") == -1)
            {
                return false;
            }
            string extname = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();
            return (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "bmp" || extname == "gif");
        }
        /// <summary>
        /// 判断是否为数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNumber(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            Regex rx = new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");
            return rx.IsMatch(value.ToString());
        }
        public static bool IsNumberArray(string[] strNumber)
        {
            if (strNumber == null) return false;
            if (strNumber.Length < 1) return false;
            foreach (string id in strNumber)
            {
                if (!KConvert.IsNumber(id))
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 判断是否为整数数组
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static bool IsIntArray(string[] strNumber)
        {
            if (strNumber == null) return false;
            if (strNumber.Length < 1) return false;
            foreach (string id in strNumber)
            {
                if (!KConvert.IsInt(id))
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 判断是否为Unicode
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsChar(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            try
            {
                Convert.ToChar(value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断是否为 8 位无符号整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsByte(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            try
            {
                Convert.ToByte(value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断是否为 16 位无符号整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsShort(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            try
            {
                Convert.ToInt16(value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断是否为 32位 整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsInt(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            Regex rx = new Regex(@"^[+-]?[0123456789]+$");
            return rx.IsMatch(value.ToString());
        }
        /// <summary>
        /// 判断是否为 单精度浮点类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsSingle(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            try
            {
                Convert.ToSingle(value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断是否为 高精度浮点类型 128位
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDecimal(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            try
            {
                Convert.ToDecimal(value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断是否为 64位 整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsLong(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            try
            {
                Convert.ToInt64(value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断是否为 双精度浮点类型 64位
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDouble(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            try
            {
                Convert.ToDouble(value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断是否为DateTime类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDateTime(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            try
            {
                DateTime dt = DateTime.Parse(value.ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool IsTime(object value)
        {
            if (!KConvert.IsEmpty(value)) return false;
            return Regex.IsMatch(value.ToString(), @"^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
        }
        public static bool IsBoolean(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            try
            {
                Convert.ToBoolean(value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool IsIPV4(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            return Regex.IsMatch(value.ToString(), @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
        public static bool IsBase64String(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            return Regex.IsMatch(value.ToString(), @"[A-Za-z0-9\+\/\=]");
        }
        public static bool IsSafeSqlString(object value)
        {
            if (KConvert.IsEmpty(value)) return false;
            return !Regex.IsMatch(value.ToString(), @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
        }
        #endregion
    }
}
